< previous page page_224 next page >

Page 224
        dwType As Long
        dwDisplayType As Long
        dwUsage As Long
        lpLocalName As Long
        lpRemoteName As Long
        lpComment As Long
        lpProvider As Long
End Type
This creates two new problems.
First, before calling the WNetGetResourceInformation function, you need to set the lpRemoteName parameter to a pointer to a NULL-terminated ANSI string. That means that you must create an ANSI string buffer and obtain its address explicitly.
One of the easiest ways to create memory buffers is by using byte arrays.
Here's a code segment that can load a dynamically allocated byte array with a NULL-terminated ANSI string:
' Load a dynamically allocated byte array with an ANSI string.
Private Sub CreateAnsiArray(StringVar As String, ByteArray() As Byte)
   ReDim ByteArray(Len(StringVar))
   Call lstrcpy(VarPtr(ByteArray(0)), StringVar)
End Sub
There's a subtle technique here.
First, exactly how long is the byte array after the ReDim operation?
If the result of the Len(StringVar) operation was 5, the ByteArray parameter will be redimensioned with an index of 5. But in Visual Basic, this creates a byte array that is 6 bytes long! That extra byte will be used to hold the NULL termination character.
The lstrcpy function is defined as follows:
Private Declare Function lstrcpy Lib "kernel32" Alias "lstrcpyA" _
(ByVal lpString1 As Long, ByVal lpString2 As String) As Long
The lstrcpy function is designed to copy strings from a source pointer to a destination pointer. The destination pointer (lpString1) is specified as a Long so that we can pass the pointer that is obtained using the VarPtr operator. The lpString2 parameter is declared ByVal as a string. What happens when you pass a VB string to an API function using the ByVal operator? It is converted into a NULL-terminated ANSI string!which is exactly what the lstrcpy function expects to see as a parameter.

 
< previous page page_224 next page >